
package com.pace.gonu.api;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pace.gonu.constants.EndType;
import com.pace.gonu.constants.Order;
import com.pace.gonu.exception.NothingException;
import com.pace.gonu.model.chnl.PlayChannel;
import com.pace.gonu.play.PlayController;
import com.pace.gonu.play.PlayService;

/** 
 * public 메서드의 경우 chnlnumber는 언제나 두번째 자리에 와야 된다. 
 * CheckLastAccessDateAspect에서 사용함
 * @author pace
 */
@Controller
@RequestMapping(value = "/gonu/play/api")
public class UserInputController {
	private static final Logger LOG = LoggerFactory.getLogger(PlayController.class);
	@Autowired
	private UserInputService userInputService;
	@Autowired
	private PlayService playService;

	@RequestMapping(value = "/withdraw.json", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap withdraw(HttpSession session, int chnlNumber) {
		ModelMap mm = new ModelMap();
		String order = (String) session.getAttribute("ORDER");
		PlayChannel playChannel = playService.getChannel(chnlNumber);
		playChannel.setEndType(EndType.WITHDRAW);
		Order.otherOrder(Order.valueOf(order));
		playChannel.setWinner(Order.otherOrder(Order.valueOf(order)));

		mm.addAttribute("message", "ok");
		return mm;
	}

	@RequestMapping(value = "/movePath.json", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap getMovePath(HttpSession session, int chnlNumber, int rockNumber) {
		ModelMap mm = new ModelMap();
		String order = (String)session.getAttribute("ORDER");
		try {
			mm.addAttribute("movePath", userInputService.getMovePath(order, chnlNumber, rockNumber));
		} catch (IllegalStateException e) {
			mm.addAttribute("message", e.getMessage());
		}

		mm.addAttribute("message", "ok");
		return mm;
	}

	@RequestMapping(value = "/moveRock.json", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap moveRock(HttpSession session, int chnlNumber, int rockNumber, int x, int y) {
		ModelMap mm = new ModelMap();
		try {
			userInputService.moveRock(chnlNumber, rockNumber, x, y, (String)session.getAttribute("ORDER"));
		} catch (NothingException ne) {
			LOG.error(ne.getMessage());
		} catch (Exception e) {
			mm.addAttribute("message", e.getMessage());
			return mm;
		}

		mm.addAttribute("message", "ok");
		return mm;
	}

	/**
	 * 이동인 아닌 돌을 놓음
	 */
	@RequestMapping(value = "/choiceMoveRock.json", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap choiceMoveRock(HttpSession session, int chnlNumber, int moveRockNumber) {
		ModelMap mm = new ModelMap();
		try {
			userInputService.choiceMoveRock(chnlNumber, moveRockNumber, (String)session.getAttribute("ORDER"));
		} catch (NothingException ne) {
			LOG.error(ne.getMessage());
		} catch (Exception e) {
			mm.addAttribute("message", e.getMessage());
			return mm;
		}

		mm.addAttribute("message", "ok");
		return mm;
	}

	@RequestMapping(value = "/choiceXRock.json", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap choiceXRock(HttpSession session, int chnlNumber, int xRockNumber) {
		ModelMap mm = new ModelMap();
		try {
			userInputService.choiceXRock(chnlNumber, xRockNumber, (String)session.getAttribute("ORDER"));

		} catch (Exception e) {
			mm.addAttribute("message", e.getMessage());
			return mm;
		}

		mm.addAttribute("message", "ok");
		return mm;
	}

}
